package com.example.doublepink.networkmonitoring;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by doublepink on 16/9/5.
 */
public class NetworkStateService extends Service{

        private static final String tag="tag";
        private ConnectivityManager connectivityManager;
        private NetworkInfo info;
    private PhoneStateListener MyListener;


    private BroadcastReceiver mReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {

                //监听网络信号变化
                String action = intent.getAction();
                if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                    Log.d(tag, "网络状态已经改变");
                    connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
                    info = connectivityManager.getActiveNetworkInfo();

                    //监听wifi信号强弱
                    WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
                    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                    int signalLevel = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), 5);

                    if(info != null && info.isAvailable()) {
                        String name = info.getTypeName();

                        Log.d(tag, "当前网络名称：" + name);
                        Log.d(tag, "当前网络强度：" + signalLevel);
                        //doSomething()
                    } else {
                        Log.d(tag, "没有可用网络");

                        //跳转到网络设置页面
//                        Intent wifiSettingsIntent = new Intent("android.settings.WIFI_SETTINGS");
//                        startActivity(wifiSettingsIntent);
                        //doSomething()
                    }




                    TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
                    telephonyManager.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
                    MyListener = new PhoneStateListener() {

                        @Override
                        public void onSignalStrengthChanged(int asu) {
                            // TODO Auto-generated method stub
                            super.onSignalStrengthChanged(asu);
                            Log.d("ok", ""+asu);
                        }
                        @Override
                        public void onSignalStrengthsChanged(SignalStrength signalStrength) {
                            // TODO Auto-generated method stub
                            super.onSignalStrengthsChanged(signalStrength);
                            if (signalStrength.isGsm()) {
                                int length =signalStrength.getGsmSignalStrength();
                                Log.d(tag, "当前移动网络信号强度：" + length);
                            }
                        }
                    };
                }
            }
        };



        @Override
        public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void onCreate() {
            super.onCreate();
            IntentFilter mFilter = new IntentFilter();
            mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
            registerReceiver(mReceiver, mFilter);
            initGPS();
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            unregisterReceiver(mReceiver);
        }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            return super.onStartCommand(intent, flags, startId);
        }

    private void initGPS() {
        LocationManager locationManager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);
        // 判断GPS模块是否开启，如果没有则开启
        if (!locationManager
                .isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
            Toast.makeText(this, "请打开GPS",
                    Toast.LENGTH_SHORT).show();
        } else {
            // 弹出Toast
            Toast.makeText(this, "GPS is ready",
                    Toast.LENGTH_LONG).show();
            Log.d(tag, "GPS已经开启：");
        }
    }
}
